package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: 李旭
 * @Time: 15:04
 * @Description:
 */
@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    //搜索页面  http://list.gmall.com/list.html?keyword....  同步  浏览器URL路径
    @GetMapping("/list.html")
    public String list(SearchParam searchParam, Model model){
        //1:入参对象回显
        model.addAttribute("searchParam",searchParam);
        //搜索ES索引库得到的结果
        SearchResponseVo searchResponseVo = listFeignClient.list(searchParam);
        //2:回显品牌集合
        model.addAttribute("trademarkList",searchResponseVo.getTrademarkList());
        //3:回显平台属性集合
        model.addAttribute("attrsList",searchResponseVo.getAttrsList());
        //4:回显商品集合
        model.addAttribute("goodsList",searchResponseVo.getGoodsList());
        //5：当前页
        model.addAttribute("pageNo",searchResponseVo.getPageNo());
        //6: 总页数
        model.addAttribute("totalPages",searchResponseVo.getTotalPages());
        //7:urlParam  拼接当前已经选中了条件对象
        String urlParam = buildUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);
        //8:排序回显页面
        Map<String,String> orderMap = buildOrderMap(searchParam);
        model.addAttribute("orderMap",orderMap);
        //9:回显品牌
        String trademarkParam = buildTrademarkParam(searchParam);
        model.addAttribute("trademarkParam",trademarkParam);
        //10: 回显平台属性   平台属性名称:平台属性值
        List<Map> propsParamList = buildPropsParamList(searchParam);
        model.addAttribute("propsParamList",propsParamList);
        //页面渲染
        return "list/index";
    }

    //回显平台属性   平台属性名称:平台属性值
    private List<Map> buildPropsParamList(SearchParam searchParam) {

        String[] props = searchParam.getProps();
        if(null != props && props.length > 0){
            //props=23:4G:运行内存
            return Arrays.stream(props).map(prop -> {
                String[] p = prop.split(":");
                Map map = new HashMap();
                map.put("attrId",p[0]);
                map.put("attrValue",p[1]);
                map.put("attrName",p[2]);
                return map;
            }).collect(Collectors.toList());
        }
        return null;
    }

    //构建品牌回显
    private String buildTrademarkParam(SearchParam searchParam) {

        //trademark=2:华为
        String trademark = searchParam.getTrademark();
        if(!StringUtils.isEmpty(trademark)){
            String[] t = trademark.split(":");
            return "品牌:" + t[1];
        }
        return null;
    }

    //构建排序字段
    private Map<String, String> buildOrderMap(SearchParam searchParam) {
        Map<String, String> orderMap = new HashMap<>();

        String order = searchParam.getOrder();

        if(StringUtils.isEmpty(order)){
            //默认值  按照 综合排序  DESC
            orderMap.put("type","1");
            orderMap.put("sort","desc");

        }else{
            // order=1:asc  排序规则   0:asc
            String[] o = order.split(":");
            orderMap.put("type",o[0]);
            orderMap.put("sort",o[1]);
        }
        return orderMap;
    }

    //URL路径拼接
    private String buildUrlParam(SearchParam searchParam) {
        //SpringMVC  参数绑定  数组
        //http://list.gmall.com/list.html?keyword=手机&t=1:TCL&props=网络:电信2G&props=摄像:后置&
        StringBuilder sb = new StringBuilder();
        sb.append("http://list.gmall.com/list.html?");

        String keyword = searchParam.getKeyword();
        //关键词
        if(!StringUtils.isEmpty(keyword)){
            sb.append("keyword=").append(keyword);
        }
        String trademark = searchParam.getTrademark();
        if(!StringUtils.isEmpty(trademark)){
            sb.append("&trademark=").append(trademark);
        }
        String[] props = searchParam.getProps();
        if(null != props && props.length > 0){
            for (String prop : props) {
                sb.append("&props=").append(prop);
            }
        }
        return sb.toString();
    }
}
